﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP.Common.Results;
using Volo.Abp.Application.Services;
using ERP.Entity;
using ERP.UserLogin.IService;
using Volo.Abp.Domain.Repositories;
using ERP.Team.RBAC.xzc;

namespace ERP.UserLogin.Service
{
    /// <summary>
    /// 登陆调用接口
    /// </summary>
    public class LoginService : ApplicationService, ILoginService
    {
        /// <summary>
        /// 用户仓储接口
        /// </summary>
        private readonly IRepository<UserModel, int> repository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository">用户仓储接口</param>
        public LoginService(IRepository<UserModel, int> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>返回登陆人的数据</returns>
        public async Task<APIResult<UserModel>> LoginQuery(string username, string password)
        {
            var user = await repository.GetListAsync(x => x.UserName == username && x.UserPassWord == password);
            APIResult<UserModel> res = new APIResult<UserModel>()
            {
                Date = user.FirstOrDefault(),
                Message = EnumResult.登陆成功.ToString(),
                Code = EnumResult.登陆成功
            };
            if (user.Count() <= 0)
            {
                res.Date = null;
                res.Message = EnumResult.登陆失败.ToString();
                res.Code = EnumResult.登陆失败;
            }
            return res;
        }
    }
}
